package wavRead;

import FFT.*;
import fingerStore.DBOpener;
import fingerStore.ShazamHash;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;

/**
 * Created by 77430 on 2016/11/3.
 */
public class AudioResolution {

    public static File[] getAudioFileName() {
        System.out.println("请输入音频文件夹的路径");
        Scanner sc = new Scanner(System.in);
        while (true) {
            String s = sc.nextLine();
            File autioFileName = new File(s);
            if (autioFileName.isAbsolute()) {
                return autioFileName.listFiles();
            } else {
                System.out.println("请输入正确的文件名");
            }
        }
    }
    
    
    public static void main(String[] args) {
        File wavFile[] = getAudioFileName();

        for(int i =0;i<wavFile.length;++i) {
            WavFileReader wav = new WavFileReader(wavFile[i]);
            double[] freqDomain = wav.getData();
            Object[] subAray = WavFileReader.splitAry(freqDomain, Fft.WINDOW_SIZE);
            FingerPrint newPrint = new FingerPrint();
            for (Object item : subAray
                    ) {
                double[] domain = (double[]) item;
                newPrint.append(domain);
            }
            ArrayList<ShazamHash> hashes = newPrint.combineHash();

            DBOpener opener = new DBOpener();
            opener.insertSong(wavFile[i].getName());
            System.out.println(wavFile[i].getName()+"***************");
            opener.insertSongFinger(wavFile[i].getName(), hashes);
        }




        /*try {
            PrintWriter out = new PrintWriter(new FileWriter(new File("test.csv")));
            for (int i = 0; i < r.length; ++i) {
                out.printf("%.2f,\n", r[i]);
            }
            out.flush();
        } catch (IOException e) {
            e.printStackTrace();
        }*/
    }
}
